#include "stdafx.h"
#include "PlayerProjectileController.h"
#include "ControlContext.h"


PlayerProjectileController::PlayerProjectileController(PlayerProjectileControlLogic* controlLogic)
{
	playerProjectileControlLogic = controlLogic;
}

bool PlayerProjectileController::apply(EntityControl* entity, const ControlContext& context)
{
	for(std::map<std::string,Launcher*>::iterator i = playerProjectileControlLogic->getLauncherMap()->begin(); i!=playerProjectileControlLogic->getLauncherMap()->end(); i++)
	{

		Launcher *launcher = (*i).second;
		launcher->setCooldownRemaining(launcher->getCooldownRemaining() - context.dt);

		if(0<context.controlStatus.getBoundKeyValue((*i).first))
		{
			


			if( launcher->getCooldownRemaining() <= 0  && entity->isPlaying())
			{


				IntEntityParameter param(SCORE_PARAMETER,((IntEntityParameter*)entity->getParameter(SCORE_PARAMETER))->getIntValue()-100);
				entity->setParameter(SCORE_PARAMETER,&param);
				// Create new Projectile from current launcher
				Controller::Projectile newProjectile(launcher->getProjectile());

				Ogre::Vector3 projDir = entity->getMasterBody()->getFacingDir();
				projDir.normalise();
  				newProjectile.velocity =( projDir*(-1))* newProjectile.velocity;

				projectileQueue.push(newProjectile);

				launcher->resetCooldown();
			}

		}

	}

	return true;


}

Ogre::String PlayerProjectileController::getType()
{
	return "PlayerProjectileController";
}

PlayerProjectileController::~PlayerProjectileController(void)
{

}

bool PlayerProjectileController::isSpawningNeeded()
{
	if(projectileQueue.size()>0)
	{
		return true;
	}
}

Controller::Projectile PlayerProjectileController::getNextSpawnItem()
{
	Projectile nextProjectile = projectileQueue.front();
	projectileQueue.pop();

	return nextProjectile;

}